<!--
 * @Author: shenya
 * @Date: 2023-09-08 11:38:36
 * @Description: 新增话题dialog
-->
<template>
  <el-dialog
    :title="action === 'add' ? '新增话题' : '编辑话题'"
    :visible.sync="visible"
    width="50%"
    append-to-body
  >
    <avue-form
      ref="addForm"
      v-model="form"
      :option="topicOption"
      :upload-error="uploadError"
      :upload-after="uploadAfter"
      :upload-before="uploadBefore"
      :upload-change="$uploadChange"
    >
      <template #classifyId>
        <el-select v-model="form.classifyId">
          <el-option
            v-for="item in list"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </template>
    </avue-form>
    <template #footer>
      <el-button
        type="primary"
        :loading="loading"
        @click="submitTopic"
      >
        提交
      </el-button>
      <el-button @click="visible = false">取消</el-button>
    </template>
  </el-dialog>
</template>
<script>
import { topicOption } from '@/const/crud/community-content/topic';
import {
  selectTopic,
  insertTopicList,
  selectTopicListById,
  updateTopicList,
} from '@/api/core/topic';
export default {
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    action: {
      type: String,
    },
    nodeObj: {
      type: Object,
    },
    nodeIndex: {
      type: String,
    },
    id: {
      type: String,
    },
  },
  data() {
    return {
      form: {},
      loading: false,
      list: [],
    };
  },
  computed: {
    visible: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit('input', val);
      },
    },
    topicOption() {
      return topicOption(this.action === 'add');
    },
  },
  watch: {
    visible(val) {
      if (val) {
        this.selectTopic();
        if (this.action === 'edit') {
          this.getDetail();
        } else {
          this.form.classifyId = this.nodeObj.id
            ? this.nodeObj.id
            : this.nodeIndex;
        }
      } else {
        this.$refs.addForm.resetFields();
      }
    },
  },
  methods: {
    async uploadBefore(file, done, loading, column) {
      const isType =
        file.type === 'image/png' ||
        file.type === 'image/jpg ' ||
        file.type === 'image/jpeg';
      if (!isType) {
        loading();
        this.$message.error('仅支持上传JPG、PNG格式图片!');
        return;
      }
      done();
    },
    selectTopic() {
      selectTopic().then((res) => {
        this.list = res.data.data;
      });
    },
    getDetail() {
      selectTopicListById(this.id).then((res) => {
        this.form = res.data.data;
      });
    },
    //上传之前
    async uploadAfter(file, done, loading, column) {
      this.$refs.addForm.clearValidate('pic');
      done();
    },
    uploadError(error, column) {
      if (error === '文件太大不符合') {
        this.$message.warning(`文件大小不能超过${column.fileSize / 1024}M`);
      }
    },
    submitTopic() {
      this.$refs.addForm.validate(async (valid, done) => {
        if (valid) {
          if (this.action === 'add') {
            this.loading = true;
            insertTopicList(this.form)
              .then((res) => {
                if (!res.data) return;
                this.$emit('refresh');
                this.visible = false;
                this.loading = false;
                this.$message.success('新增成功！');
                done();
              })
              .catch((err) => {
                this.loading = false;
                done();
                throw new Error(err);
              });
          } else {
            this.loading = true;
            const params = {
              id: this.id,
              classifyId: this.form.classifyId,
              pic: this.form.pic,
              sort: this.form.sort,
              content: this.form.content,
            };
            updateTopicList(params)
              .then((res) => {
                if (!res.data) return;
                this.$emit('refresh');
                this.visible = false;
                this.loading = false;
                this.$message.success('编辑成功！');
                done();
              })
              .catch((err) => {
                this.loading = false;
                done();
                throw new Error(err);
              });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped></style>
